import 'package:myapp/api/online_repository.dart';
import 'package:myapp/bean/article.dart';
import 'package:myapp/globle/const.dart';
import 'package:myapp/view_model/base_model.dart';

class HomePageModel extends BaseModel {
  List<Article> _articles = [];
  List<Article> _topArticles = [];
  bool _isLoadMore = false;

  List<Article> get getTopArticles => _topArticles;

  List<Article> get getArticles => _articles;

  bool get isLoadMore => _isLoadMore;

  int _page = 0;

  refresh() async {
    _page = 0;
    var topArticles = await OnlineRepository.fetchTopArticles();
    var articles = await OnlineRepository.fetchArticles(_page);
    _articles.clear();
    topArticles?.forEach((element) {
      element.top = true;
    });
    _articles.addAll(topArticles);
    _articles.addAll(articles);
    _isLoadMore = _articles.length >= Const.PAGE_NUM;
    notifyListeners();
  }

  loadMore() async {
    _page++;
    List<Article> articles = await OnlineRepository.fetchArticles(_page);
    _articles.addAll(articles);
    _isLoadMore = articles.length >= Const.PAGE_NUM;
    notifyListeners();
  }

  @override
  void dispose() {
    super.dispose();
    print("SearchPageModel-->>dispose");
  }
}
